package com.ktg.mes.pro.domain;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ktg.mes.pro.service.IProRouteProcessService;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;
import com.ktg.common.annotation.Excel;
import com.ktg.common.core.domain.BaseEntity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工艺组成对象 pro_route_process
 *
 * @author yinjinlu
 * @date 2022-05-13
 */

@Data
@Accessors(chain = true)
@TableName("pro_route_process")
public class ProRouteProcess extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /** 记录ID */
    @TableId(value = "record_id", type = IdType.AUTO)
    private Long recordId;

    /** 工艺路线ID */
    @Excel(name = "工艺路线ID")
    private Long routeId;

    /** 工序ID */
    @Excel(name = "工序ID")
    private Long processId;

    /** 工序编码 */
    @Excel(name = "工序编码")
    private String processCode;

    /** 工序名称 */
    @Excel(name = "工序名称")
    private String processName;



    /** 序号 */
    @Excel(name = "序号")
    private Integer orderNum;

    private Integer groupNum;


    /** 工序ID */
    @Excel(name = "工序ID")
    private Long nextProcessId;

    /** 工序编码 */
    @Excel(name = "工序编码")
    private String nextProcessCode;

    /** 工序名称 */
    @Excel(name = "工序名称")
    private String nextProcessName;

    /** 与下一道工序关系 */
    @Excel(name = "与下一道工序关系")
    private String linkType;

    /** 准备时间 */
    @Excel(name = "准备时间")
    private Long defaultPreTime;

    /** 等待时间 */
    @Excel(name = "等待时间")
    private Long defaultSufTime;

    /** 甘特图显示颜色 */
    @Excel(name = "甘特图显示颜色")
    private String colorCode;

    /** 是否检测工序 */
    private Integer isTesting;

    /** 检测模板id */
    private Long templateId;

    /** 检测模板模板 */
    private String templateName;

    /** 是否关键工序 */
    private String keyFlag;

    /** 预留字段1 */
    private String attr1;

    /** 预留字段2 */
    private String attr2;

    /** 预留字段3 */
    private Long attr3;

    /** 预留字段4 */
    private Long attr4;


    /**
     * 工艺配置参数
     */
    private String paramJson;




    private static Map<String,Map<Long,JSONObject>> processToParamJson = new HashMap<>();

    public static JSONObject getProcessToParamJson(String deviceId, String routeId, Long processId, IProRouteProcessService proRouteProcessService) {
        Map<Long,JSONObject> map = processToParamJson.get(deviceId);
        if (map == null || map.get(processId) == null){
            QueryWrapper<ProRouteProcess> proRouteProcessQueryWrapper = new QueryWrapper<>();
            proRouteProcessQueryWrapper.eq("route_id",routeId);
            List<ProRouteProcess> list = proRouteProcessService.list(proRouteProcessQueryWrapper);
            if (map != null){
                map.clear();
            }
            map = new HashMap<>();
            for (int i = 0; i < list.size(); i++) {
                String paramJson1 = list.get(i).getParamJson();
                if (!StringUtils.isEmpty(paramJson1) && paramJson1.length() > 5){
                    map.put(list.get(i).getProcessId(),JSONObject.parseObject(paramJson1));
                }
            }
            processToParamJson.put(deviceId,map);
        }
        return map.get(processId);
    }
}
